//
//  LKShopSettingViewController.m
//  Mideleague_Shop
//
//  Created by Zhiwang on 2020/7/10.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKShopSettingViewController.h"
#import "LKShopSettingModel.h"
#import "LKAddQrcodeViewController.h"
#import "LKAddShopShowViewController.h"
#import "LKTimeDatePickerView.h"
@interface LKShopSettingViewController ()
@property(nonatomic,strong)UFFormView *formView;
@property(nonatomic,strong)LKShopSettingModel *model;
@property(nonatomic,copy)NSString *codeString;
@property(nonatomic,copy)NSString *picString;
@property(nonatomic,copy)NSString *open_time_en;
@property(nonatomic,copy)NSString *open_time_st;
@end

@implementation LKShopSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.codeString = @"";
    self.picString = @"";
    self.navigationItem.title = @"店铺设置";
    [self getShopDataInformation];
    // Do any additional setup after loading the view.
}

-(void)createFormView{
    WS(weakSelf)
    self.formView = [UFFormView makeFormView:^(UFFormViewMaker * _Nonnull make) {
        make
        .addSection([UFSection makeSection:^(UFSectionMaker * _Nonnull make) {
            make
            .titleStyle([UFRowTitleStyle makeTitleStyle:^(UFRowTitleStyleMaker * _Nonnull make) {
               make
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor)
                .textAlignment(NSTextAlignmentLeft);
            }])
            .valueStyle([UFTextStyle makeTextStyle:^(UFRowTextStyleMaker * _Nonnull make) {
                make
                .textAlignment(NSTextAlignmentRight)
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor);
            }])

            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .editable(NO)
                .height(RowHeightDefault)
                .title(@"营业时间")
                .accessoryType(UFRowAccessoryDisclosureIndicator)
                .rowDidSelected(^(__kindof UFRow * _Nonnull row) {
                    [LKTimeDatePickerView showCustomDatePickerAtView:self.view WithFomate:@"" choosedDateBlock:^(NSDate * _Nonnull date, NSDate * _Nonnull rightdate) {
                        NSString *startTimeStr = [[NSString getTimeStrAccoringDate:date] componentsSeparatedByString:@" "][1];
                        NSString *endTimeStr = [[NSString getTimeStrAccoringDate:rightdate] componentsSeparatedByString:@" "][1];
                        weakSelf.model.open_time_st = [startTimeStr substringToIndex:5];
                        weakSelf.model.open_time_en = [endTimeStr substringToIndex:5];
                        NSMutableDictionary *param = [[NSMutableDictionary alloc] initWithDictionary:@{@"open_time_st":weakSelf.model.open_time_st,@"open_time_en":weakSelf.model.open_time_en}];
                         NSString *sign = [NSString encoingWithDic:param];
                         [param setObject:sign forKey:@"sign"];
                         [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyShopOpenTimeUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
                             
                         } success:^(BOOL isSuccess, id  _Nullable responseObject) {
                             [SVProgressHUD dismiss];
                             if ([responseObject[@"code"] isEqualToString:@"success"]) {
                                 [weakSelf.formView removeFromSuperview];
                                 [weakSelf createFormView];
                             }else{
                                 [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                             }
                         } failure:^(NSString * _Nullable errorMessage) {
                             
                         }];
                    } cancelBlock:^{
                        
                    }];

                })
                .value([NSString stringWithFormat:@"%@~%@",weakSelf.model.open_time_st,weakSelf.model.open_time_en]);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .height(RowHeightDefault)
                .title(@"门店电话")
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                })
                .value(weakSelf.model.kefu_phone);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .editable(NO)
                .height(RowHeightDefault)
                .accessoryType(UFRowAccessoryDisclosureIndicator)
                .title(@"微信客服二维码")
                .rowDidSelected(^(__kindof UFRow * _Nonnull row) {
                    LKAddQrcodeViewController *vc = [[LKAddQrcodeViewController alloc] init];
                    vc.block = ^{
                        [weakSelf.formView removeFromSuperview];
                        [weakSelf getShopDataInformation];
                    };
                    vc.qrImageUrl = weakSelf.model.qrcode;
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                })
                .value(weakSelf.codeString);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .editable(NO)
                .placeholder(@"")
                .height(RowHeightDefault)
                .accessoryType(UFRowAccessoryDisclosureIndicator)
                .title(@"店铺展示图")
                .rowDidSelected(^(__kindof UFRow * _Nonnull row) {
                    LKAddShopShowViewController *vc = [[LKAddShopShowViewController alloc] init];
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                })
                .value(weakSelf.picString);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .editable(NO)
                .placeholder(@"去后台设置")
                .height(RowHeightDefault)
                .title(@"店铺介绍")
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                })
                .value(@"已设置");
            }]);
        }])
        .addToSuperView(self.view);
    }];

    [self.formView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).mas_equalTo(NAVIGATION_BAR_HEIGHT + 10);
        make.height.mas_equalTo(260);
    }];
    self.formView.scrollEnabled = NO;

}

/// 获取商铺信息
-(void)getShopDataInformation{
    NSMutableDictionary *param = [[NSMutableDictionary alloc] initWithDictionary:@{}];
    NSString *sign = [NSString encoingWithDic:param];
    [param setObject:sign forKey:@"sign"];
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyShopSetUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        [SVProgressHUD dismiss];
        if ([responseObject[@"code"] isEqualToString:@"success"]) {
            NSDictionary *dic = responseObject[@"data"];
            self.model = [LKShopSettingModel modelWithDictionary:dic];
            if (self.model.qrcode.length != 0) {
                self.codeString = @"已设置";
            }else{
                self.codeString = @"请选择微信客服二维码图";
            }
            if ([self.model.upload_img isEqualToString:@"1"]) {
                self.picString = @"已设置";
            }else{
                self.picString = @"请选择店铺展示图";
            }
            
            [self createFormView];
        }else{
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
        }
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
